package com.yaoweigroup.community.adapter;

import java.util.List;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.yaoweigroup.community.R;
import com.yaoweigroup.community.config.Urls;
import com.yaoweigroup.community.enity.Adv;
import com.yaoweigroup.community.ui.bm.DetailActivity;
import com.yaoweigroup.community.ui.home.AnnounceDetailActivity;
import com.yaoweigroup.community.ui.shop.ShopDetailActivity;
import com.yaoweigroup.community.util.BitmapHelp;

import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

public class MainFocusAdapter extends PagerAdapter {

	private LayoutInflater inflater;
	private Context context;
	private List<Adv> list;
	public static int test = 0;
	private BitmapUtils bitmapUtils;
	private BitmapDisplayConfig bigPicDisplayConfig;

	public MainFocusAdapter(List<Adv> list, Context context) {
		this.context = context;
		this.list = list;
		inflater = LayoutInflater.from(context);
		bitmapUtils = BitmapHelp.getBitmapUtils(context);
		bigPicDisplayConfig = new BitmapDisplayConfig();
		bigPicDisplayConfig.setLoadFailedDrawable(context.getResources()
				.getDrawable(R.drawable.bucket_no_picture));
		bigPicDisplayConfig.setLoadingDrawable(context.getResources()
				.getDrawable(R.drawable.bucket_no_picture));
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		View imageLayout = inflater.inflate(R.layout.focus_view, container,
				false);
		ImageView imageView = (ImageView) imageLayout
				.findViewById(R.id.imageView);
		((ViewPager) container).addView(imageLayout);

		final Adv adv = list.get(position);
		bitmapUtils.display(imageView, adv.getImageurl(), bigPicDisplayConfig);
		imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

//				Intent intent;
//				if (adv.getTypename().equals("商品")) {
//					// 商品
//					intent = new Intent(context, ShopDetailActivity.class);
//					intent.putExtra("id", adv.getValue());
//					context.startActivity(intent);
//				} else if (adv.getTypename().equals("商户")) {
//					intent = new Intent(context, DetailActivity.class);
//					intent.putExtra("id", adv.getValue());
//					context.startActivity(intent);
//					// 商户
//				} else if (adv.getTypename().equals("公告")) {
//					// 公告
//					intent = new Intent(context, AnnounceDetailActivity.class);
//					intent.putExtra("id", adv.getValue());
//					context.startActivity(intent);
//				}
			}
		});
		return imageLayout;
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {

	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View arg0) {

	}

	@Override
	public void finishUpdate(View arg0) {

	}

	@Override
	public void destroyItem(View container, int position, Object object) {
		((ViewPager) container).removeView((View) object);

	}

	public void setDataChange(List<Adv> list) {
		this.list = list;
		notifyDataSetChanged();
	}
}